package cn.itcast.usermanage.controller;

import cn.itcast.usermanage.pojo.EasyResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class TestController {

    /**
     * @Description:通用页面方法
     *
     * @Author:fsy
     * @Date:2018/3/12
     */
    @RequestMapping("views/{viewName}")
    public ModelAndView show(@PathVariable("viewName") String viewName) {
        return new ModelAndView(viewName);
    }

    //    注入service
    @Autowired
    private UserService userService;


    /**
     * @Description:分页及list页面，不用restful风格的
     * url:user/list
     * 跳转的时候：异步刷新
     * 参数：rows,page,必须要的
     * 返回结果：list<User>,总记录数（total）
     * 步骤：
     * @Author:fsy
     * @Date:2018/3/12
     */
    /* @ResponseBody
    @RequestMapping("list")
   public Map<String, Object> queryUserPageList(@RequestParam(value = "rows", defaultValue = "1") Integer rows,
                                                 @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> resultMap = new HashMap<>();
//        统计数据库中所有用户的数量
        Long total = this.userService.count();
//        分页查询数据库中的数据
        List<User> userList = this.userService.queryList((page - 1) * rows, rows);
//        根据参数将数据返回到页面上
        resultMap.put("total", total);
        resultMap.put("rows", userList);
        return resultMap;
    }*/

    /**
    * @Description:使用restful风格
    *
    * @Author:fsy
    * @Date:2018/3/13
    */
    @RequestMapping(value="list",method = RequestMethod.POST)
    public ResponseEntity<EasyResult> queryUserPageList(@RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page) {
        try {
//            List<User> users = this.userService.queryList(page, rows);
//            Long total = this.userService.count();
//            EasyResult easyResult = new EasyResult(users,total);
            PageInfo<User> pageInfo = this.userService.queryUserAll(page, rows);
            EasyResult easyResult = new EasyResult(pageInfo.getList(), pageInfo.getTotal());
            return ResponseEntity.status(HttpStatus.OK).body(easyResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * @Description:
     * url:user/save
     * 参数：user，必须
     * 同步加载
     * 返回结果 ：数据显示在分页的列表上
     * @Author:fsy
     * @Date:2018/3/13
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public  Map<String,Object> saveUser(User user){
        Map<String, Object> map = new HashMap<>();
        this.userService.saveUser(user);
        map.put("status",200);
        return  map;
    }

    /**
    * @Description:删除指定的用户
    *  url:/user/delete
     *  参数：ids这个ids是一个复数，因为可以同时删除多个
     *  异步加载
     *  返回状态码
     *  疑问：什么时候要使用requestParam，什么时候不用
    * @Author:fsy
    * @Date:2018/3/13
    */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteUserByIds(@RequestParam("ids") String ids){
        Map<String, Object> map = new HashMap<>();
        this.userService.delete(ids);
        map.put("status",200);
        return  map;
    }


    /**
    * @Description:文件的下载
    *   1.查询分页数据    2.写入Excel   3.下载Excel
    * @Author:fsy
    * @Date:2018/3/13
    */
    @RequestMapping("export/excel")
    public ModelAndView exportUserToExcel(@RequestParam(value = "page",defaultValue = "") Integer page,
          @RequestParam(value = "rows",defaultValue = "10")Integer rows){
        ModelAndView mv = new ModelAndView();
        List<User> userList = this.userService.queryList(page, rows);
        mv.addObject("userList",userList);
//        视图名称
        mv.setViewName("excelView");
        return  mv;
    }

}
